﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Threading;


namespace Piyrez_LoL_Jungle_Timers
{

    public partial class mainForm : Form
    {
        //Timer INTS
        
        public mainForm()
        {
            InitializeComponent();
        }

        public static Form IsFormAlreadyOpen(Type FormType)
        {
            foreach (Form openForm in Application.OpenForms)
            {
                if (openForm.GetType() == FormType)
                    return openForm;
            }
            return null;
        }

        private void buttonSettings_Click(object sender, EventArgs e)
        {
            //settingsForm SettingsForm = new settingsForm();
            //SettingsForm.Show();
            Overlay oForm = null;
            if ((oForm = (Overlay)IsFormAlreadyOpen(typeof(Overlay))) == null)
            {
                oForm = new Overlay();
                oForm.Show();
            }
            else
                //MessageBox.Show("Already open!");
                oForm.Focus();
        }

        private void purpGolemTimer_Tick(object sender, EventArgs e)
        {
            purpGolemBar.Increment(1);
            Overlay.olpurpGolemL.Text = (Program.pgTick--).ToString();
            if (purpGolemBar.Value == 270) PlaySound(Application.StartupPath + "/Audio/Purple_Golem_30.wav", 0, SND_ASYNC);
            if (purpGolemBar.Value == 300)
            {
                PlaySound(Application.StartupPath + "/Audio/Purple_Golem_Spawned.wav", 0, SND_ASYNC);
                purpGolemTimer.Stop();
                Overlay.olpurpGolemL.Text = "";
                Program.pgTick = 0;
            }
        }

        private void purpLizardTimer_Tick(object sender, EventArgs e)
        {
            purpLizardBar.Increment(1);
            Overlay.olpurpLizardL.Text = (Program.plTick--).ToString();
            if (purpLizardBar.Value == 270) PlaySound(Application.StartupPath + "/Audio/Purple_Lizard_30.wav", 0, SND_ASYNC);
            if (purpLizardBar.Value == 300)
            {
                PlaySound(Application.StartupPath + "/Audio/Purple_Lizard_Spawned.wav", 0, SND_ASYNC);
                purpLizardTimer.Stop();
                Overlay.olpurpLizardL.Text = "";
                Program.plTick = 0;
            }
        }

        private void blueGolemTimer_Tick(object sender, EventArgs e)
        {
            blueGolemBar.Increment(1);
            Overlay.olblueGolemL.Text = (Program.bgTick--).ToString();
            if (blueGolemBar.Value == 270) PlaySound(Application.StartupPath + "/Audio/Blue_Golem_30.wav", 0, SND_ASYNC);
            if (blueGolemBar.Value == 300)
            {
                PlaySound(Application.StartupPath + "/Audio/Blue_Golem_Spawned.wav", 0, SND_ASYNC);
                blueGolemTimer.Stop();
                Overlay.olblueGolemL.Text = "";
                Program.bgTick = 0;
            }
        }

        private void blueLizardTimer_Tick(object sender, EventArgs e)
        {
            blueLizardBar.Increment(1);
            Overlay.olblueLizardL.Text = (Program.blTick--).ToString();
            if (blueLizardBar.Value == 270) PlaySound(Application.StartupPath + "/Audio/Blue_Lizard_30.wav", 0, SND_ASYNC);
            if (blueLizardBar.Value == 300)
            {
                PlaySound(Application.StartupPath + "/Audio/Blue_Lizard_Spawned.wav", 0, SND_ASYNC);
                blueLizardTimer.Stop();
                Overlay.olblueLizardL.Text = "";
                Program.blTick = 0;
            }
        }

        private void DragonTimer_Tick(object sender, EventArgs e)
        {

            DragonBar.Increment(1);
            Overlay.olDragonL.Text = (Program.dTick--).ToString();
            if (DragonBar.Value == 330) PlaySound(Application.StartupPath + "/Audio/Dragon_30.wav", 0, SND_ASYNC);
            if (DragonBar.Value == 360)
            {
                PlaySound(Application.StartupPath + "/Audio/Dragon_Spawned.wav", 0, SND_ASYNC);
                DragonTimer.Stop();
                Overlay.olDragonL.Text = "";
                Program.dTick = 0;
            }
        }

        private void BaronTimer_Tick(object sender, EventArgs e)
        {
            BaronBar.Increment(1);
            Overlay.olBaronL.Text = (Program.bTick--).ToString();
            if (BaronBar.Value == 390) PlaySound(Application.StartupPath + "/Audio/Baron_30.wav", 0, SND_ASYNC);
            if (BaronBar.Value == 420)
            {
                PlaySound(Application.StartupPath + "/Audio/Baron_Spawned.wav", 0, SND_ASYNC);
                BaronTimer.Stop();
                Overlay.olBaronL.Text = "";
                Program.bTick = 0;
            }
        }

        private void overlayTimer_Tick(object sender, EventArgs e)
        {
            lolListen();
        }

        private void buttonReset_Click(object sender, EventArgs e)
        {
            purpGolemTimer.Stop();
            purpGolemBar.Value = 0;
            Overlay.olpurpGolemL.Text = "";
            Program.pgTick = 0;
            purpLizardTimer.Stop();
            purpLizardBar.Value = 0;
            Overlay.olpurpLizardL.Text = "";
            Program.plTick = 0;
            blueGolemTimer.Stop();
            blueGolemBar.Value = 0;
            Overlay.olblueGolemL.Text = "";
            Program.bgTick = 0;
            blueLizardTimer.Stop();
            blueLizardBar.Value = 0;
            Overlay.olblueLizardL.Text = "";
            Program.blTick = 0;
            DragonTimer.Stop();
            DragonBar.Value = 0;
            Overlay.olDragonL.Text = "";
            Program.dTick = 0;
            BaronTimer.Stop();
            BaronBar.Value = 0;
            Overlay.olBaronL.Text = "";
            Program.bTick = 0;
        }

        private void purpGolemPic_Click(object sender, EventArgs e)
        {
            purpGolemTimer.Enabled = true;
        }

        private void purpLizardPic_Click(object sender, EventArgs e)
        {
            purpLizardTimer.Enabled = true;
        }

        private void blueGolemPic_Click(object sender, EventArgs e)
        {
            blueGolemTimer.Enabled = true;
        }

        private void blueLizardPic_Click(object sender, EventArgs e)
        {
            blueLizardTimer.Enabled = true;
        }

        private void BaronPic_Click(object sender, EventArgs e)
        {
            BaronTimer.Enabled = true;
        }

        private void DragonPic_Click(object sender, EventArgs e)
        {
            DragonTimer.Enabled = true;
        }

        private void aboutPic_Click(object sender, EventArgs e)
        {
            //CREDITS/ABOUT/HELP WINDOW OPEN!  List keybinds here!
            AboutBox aBox = null;
            if ((aBox = (AboutBox)IsFormAlreadyOpen(typeof(AboutBox))) == null)
            {
                aBox = new AboutBox();
                aBox.Show();
            }
            else
                aBox.Focus();
        }

        private void label1_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("http://www.leagueoflegends.com");
        }

        // Playing Audio Files!

        [DllImport("winmm.dll")]
        private static extern bool PlaySound(string filename, int module, int flags);

        public int SND_SYNC = 0x0000;
        public int SND_ASYNC = 0x0001;
        public int SND_NODEFAULT = 0x0002;
        public int SND_MEMORY = 0x0004;
        public int SND_LOOP = 0x0008;
        public int SND_NOSTOP = 0x0010;
        public int SND_NOWAIT = 0x00002000;
        public int SND_ALIAS = 0x00010000;
        public int SND_ALIAS_ID = 0x00110000;
        public int SND_FILENAME = 0x00020000;
        public int SND_RESOURCE = 0x00040004;
        public int SND_PURGE = 0x0040;
        public int SND_APPLICATION = 0x0080;




        public void lolListen()
        {
            // Finding League of Legends
            Process[] processes = Process.GetProcessesByName("League of Legends");

            foreach (Process p in processes)
            {
                IntPtr pFoundWindow = p.MainWindowHandle;

                OverlayForm();

                Thread listener = new Thread(() =>
                {
                    if (null != p)
                    {
                        p.WaitForExit();
                    }
                });
                listener.Start();

            }
        }

        public void OverlayForm()
        {
            Overlay oForm = null;
            if ((oForm = (Overlay)IsFormAlreadyOpen(typeof(Overlay))) == null)
            {
                oForm = new Overlay();
                oForm.Show();
            }
        }
    }
}